package com.cema.manage.invoke;


import com.cema.manage.invoke.interfase.TestAttributeUtilsInterFase;
import com.cema.manage.invoke.interfase.TestParamUtilsInterFase;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


public class TestParamUtils implements TestParamUtilsInterFase {
    private static TestParamUtils paramUtils;
    private TestAttributeUtilsInterFase attributeUtilsInterFase;

    public static TestParamUtils getInstance() {
        if (paramUtils == null) {
            paramUtils = new TestParamUtils();
        }
        return paramUtils;
    }

    private TestParamUtils() {
        attributeUtilsInterFase = TestAttributeUtils.getInstance();
    }

    /**
     * 构造参数
     *
     * @param request
     * @param classs
     * @return
     */
    @Override
    public Object getParam(HttpServletRequest request, Class<?> classs) {
        String[] filterSts = attributeUtilsInterFase.getAttributeName(classs,
                TestAttributeUtils.FILTER_PRIVATE);
        Object obj = null;
        try {
            obj = classs.newInstance();
            for (int i = 0; i < filterSts.length; i++) {
                String setName = filterSts[i];
                String setValue = request.getParameter(setName);
                // /拼接set方法
                if (setValue != null && setValue.length() > 0) {
                    setName = "set" + setName.substring(0, 1).toUpperCase()
                            + setName.substring(1);
                    Method method = classs.getDeclaredMethod(setName,
                            String.class);
                    method.setAccessible(true);
                    method.invoke(obj, setValue);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj;
    }
}
